package com.xigua.xcar.widgets.CommonList;

import java.util.ArrayList;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xigua.xcar.R;
import com.xigua.xcar.data.CommonListData;

public class CommonListAdapter extends BaseAdapter implements OnClickListener {
	// warning:CommonListData中的数据shopName必须不为空，其他都可以为无效值
	private ArrayList<CommonListData> mArray = null;
	private LayoutInflater mInflater;
	private CommonListListener mListener = null;
	private OnClickListener mItemClick = null;
	private Context mContext;
	public CommonListAdapter(Context context) {
		if (null != context) {
			mContext = context;
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
	}

	public int getCount() {
		if (null != mArray) {
			return mArray.size();
		}
		return 0;
	}

	public Object getItem(int arg0) {
		return mArray.get(arg0 % mArray.size());
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	public void setData(ArrayList<CommonListData> array) {
		mArray = array;
		this.notifyDataSetChanged();
	}

	public void setItemClickListener(OnClickListener listener) {
		mItemClick = listener;
	}

	public void setCommonListListener(CommonListListener listener) {
		mListener = listener;
	}

	private void updateView(View convertView, CommonListData data, int index){
		TextView shopName = null;
		TextView phoneNum = null;
		ImageView image = null;
		RelativeLayout layout = null;
		shopName = (TextView) convertView
				.findViewById(R.id.txt_common_shopname);
		phoneNum = (TextView) convertView
				.findViewById(R.id.txt_common_phoneNum);
		image = (ImageView) convertView
				.findViewById(R.id.image_common_image);
		if (null == data.getmShopName() || "".equals(data.getmShopName())) {
			shopName.setVisibility(View.GONE);
		}else{
			shopName.setText(data.getmShopName());
		}
		if (null == data.getmPhoneNum() || "".equals(data.getmPhoneNum())) {
			phoneNum.setVisibility(View.GONE);
		} else {
			phoneNum.setText(data.getmPhoneNum());
		}
		if (0 != data.getmResId()) {
			image.setBackgroundResource(data.getmResId());
		} else {
			image.setVisibility(View.GONE);
		}
		convertView.setTag(data);
		convertView.setId(index);
		convertView.setOnClickListener(mItemClick);
		if(0 == index){
			convertView.setBackgroundResource(R.drawable.list_back_top);
		}else if((mArray.size()-1) == index){
			convertView.setBackgroundResource(R.drawable.list_back_bottom);
		}else{
			convertView.setBackgroundResource(R.drawable.list_back_middle);
			layout = (RelativeLayout)convertView.findViewById(R.id.layout_common_shopinfo);
			RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams)layout.getLayoutParams();
			DisplayMetrics dm;
			float scaleZoom;
			dm = new DisplayMetrics();
			WindowManager win = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
			win.getDefaultDisplay().getMetrics(dm);
			scaleZoom = dm.density;
			int offset = (int)(10*scaleZoom);
			param.setMargins(offset,offset,0,offset);
			layout.setLayoutParams(param);
			param = (RelativeLayout.LayoutParams)image.getLayoutParams();
			param.setMargins(0, offset, offset, offset);
			image.setLayoutParams(param);
		}
		if(mArray.size()==1){
			convertView.setBackgroundResource(R.drawable.about_back);
		}
	}
	
	public View getView(int arg0, View convertView, ViewGroup parent) {
		arg0 = arg0 % (mArray.size());
		CommonListData data = mArray.get(arg0);
//		if (convertView == null) 
		{
			
			convertView = mInflater.inflate(
					R.layout.commonlist_item, null);
			
		}
		updateView(convertView,data,arg0);
		return convertView;
	}

	public void onClick(View v) {
		if (null != mListener) {
			CommonListData data = (CommonListData) v.getTag();
			mListener.onClickBtn(data);
		}
	}

}
